home *** CD-ROM | disk | FTP | other *** search
- /* Scrolling Manager Library of Subroutines
- © 1988 John A. Nairn, All Rights Reserved
-
- This library of routines allow any application to
- easily implement scrolling in any window. The library
- can handle application-defined graphic windows as well
- as Text Edit windows.
-
- General variables appearing as common parameters
- 1. theScrlp: the ScrollPtr for scrolling window
- 2. theWindow: window pointer to scrolling window
-
- Routines marked “internal use” should not need to be
- called by the application
-
- WARNING: A number of these routines assume scrolling
- is occurring in the active window. This means
- that ScrollActivate() must have been called
- before calling these routines. Failure to do so
- may result in system crash.
- */
-
- #include <MacTypes.h> /* Includes */
- #include <QuickDraw.h>
- #include <ControlMgr.h>
- #include <TextEdit.h>
- #include "ScrollMgr.h"
- #define Active 0 /* defines */
- #define Inactive 255
- #define NIL 0L
-
- ScrollPtr scrlp; /* Global Variables */
- int scrollAmt,scrollCode,scrollType,linesVis,lineSize;
- int vJoySpeed=8;hJoySpeed=8;
- pascal Boolean SMClikLoop();
-
- /* Initialize ScollInfo data record for new window and
- return ScrollPtr as the function return. Note
- this routine calls ScrollActivate() to activate
- the scroll bars.
- 1. theScrlp: pointer to use for ScrollInfo or NIL to
- put ScrollInfo in heap
- 2. textWindow=TRUE if Text Edit window else =FALSE
- 3. v or hLineSize: pixels per line to scroll (0 to
- not include scroll bar)
- 4. v or hTotalLines: number of lines in document
- 5. v or hTools: width of tool bars in pixels (0 if
- no tool bar)
- 6. v or hRuler: width of rulers in pixels (0 if no
- ruler)
- 7. vScrollTop or hScrollLeft: scroll bar margins
- 8. sRefCon: ScrollInfo refCon */
-
- ScrollPtr SetScrollWindow(theWindow,theScrlp,textWindow,
- vLineSize,vTotalLines,vTools,vRuler,
- vScrollTop,hLineSize,hTotalLines,hTools,
- hRuler,hScrollLeft,sRefCon)
- WindowPtr theWindow;
- ScrollPtr theScrlp;
- Boolean textWindow;
- int vLineSize,vTotalLines,vTools,vRuler;
- int vScrollTop,hLineSize,hTotalLines,hTools;
- int hRuler,hScrollLeft;
- long sRefCon;
- {
- ScrollPtr newscrlp;
- Rect viewRect,scrollRect;
-
- if(theScrlp==NIL)
- newscrlp=(ScrollInfo *)
- NewPtr((Size)sizeof(ScrollInfo));
- else
- newscrlp=theScrlp;
-
- /* vertical scoll bar initialization */
- newscrlp->vScrollMarg=vScrollTop;
- newscrlp->vRectTopLeft.v=hTools+hRuler;
- newscrlp->vRectTopLeft.h=vTools;
- if(vLineSize!=0)
- { SetVertRect(newscrlp,theWindow,vLineSize,
- hLineSize);
- scrollRect=theWindow->portRect;
- scrollRect.left=scrollRect.right-SBarWidth;
- scrollRect.right+=1;
- scrollRect.bottom-=14;
- scrollRect.top-=1-newscrlp->vScrollMarg;
- newscrlp->vScrollHdl=NewControl(theWindow,
- &scrollRect,"\p",1,0,0,0,scrollBarProc,
- (long)vLineSize);
- SetScroll(newscrlp,vTotalLines,VertBar);
- }
- else
- newscrlp->vScrollHdl=NIL;
-
- /* horizontal scoll bar initialization */
- newscrlp->hScrollMarg=hScrollLeft;
- newscrlp->hRectTopLeft.v=hTools;
- newscrlp->hRectTopLeft.h=vTools+vRuler;
- if(hLineSize!=0)
- { SetHorizRect(newscrlp,theWindow,hLineSize,
- vLineSize);
- scrollRect=theWindow->portRect;
- scrollRect.left-=1-newscrlp->hScrollMarg;
- scrollRect.right-=SBarWidth-1;
- scrollRect.top=scrollRect.bottom-SBarWidth;
- scrollRect.bottom+=1;
- newscrlp->hScrollHdl=NewControl(theWindow,
- &scrollRect,"\p",1,0,0,0,scrollBarProc,
- (long)(-hLineSize));
- SetScroll(newscrlp,hTotalLines,HorizBar);
- }
- else
- newscrlp->hScrollHdl=NIL;
-
- /* Text edit initialization (if text window) */
- if(textWindow)
- { ScrollSectRect(newscrlp,theWindow,&viewRect);
- newscrlp->hTE=TENew(&viewRect,&viewRect);
- SetClikLoop(&SMClikLoop,newscrlp->hTE);
- }
- else
- newscrlp->hTE=NIL;
-
- newscrlp->refCon=sRefCon;
- ScrollActivate(newscrlp);
- return(newscrlp);
- }
-
- /* Set the scrolling rectangles - internal use */
-
- SetVertRect(newscrlp,theWindow,theLineSize,hasHorizBar)
- ScrollPtr newscrlp;
- WindowPtr theWindow;
- int theLineSize,hasHorizBar;
- {
- Rect r;
-
- r=theWindow->portRect;
- r.left+=newscrlp->vRectTopLeft.h;
- r.top+=newscrlp->vRectTopLeft.v;
- r.right-=SBarWidth;
- if(hasHorizBar)
- r.bottom-=SBarWidth;
- newscrlp->vertScrollRect=r;
- newscrlp->vertLinesVis=(r.bottom-r.top)/theLineSize;
- }
-
- SetHorizRect(newscrlp,theWindow,theLineSize,hasVertBar)
- ScrollPtr newscrlp;
- WindowPtr theWindow;
- int theLineSize,hasVertBar;
- {
- Rect r;
-
- r=theWindow->portRect;
- r.left+=newscrlp->hRectTopLeft.h;
- r.top+=newscrlp->hRectTopLeft.v;
- r.bottom-=SBarWidth;
- if(hasVertBar)
- r.right-=SBarWidth;
- newscrlp->horizScrollRect=r;
- newscrlp->horizLinesVis=(r.right-r.left)/theLineSize;
- }
-
- /* Call when window activates: will activate scroll bars
- and set internal pointer scrlp to newscrlp which
- is ScrollPtr for window being activated */
-
- ScrollActivate(newscrlp)
- ScrollPtr newscrlp;
- {
- scrlp=newscrlp;
- if(scrlp->vScrollHdl!=NIL)
- HiliteControl(scrlp->vScrollHdl,Active);
- if(scrlp->hScrollHdl!=NIL)
- HiliteControl(scrlp->hScrollHdl,Active);
- }
-
- /* Call when window deactivates: will deactivate scroll
- bars. oldscrlp is ScrollPtr for deactivating
- window */
-
- ScrollDeactivate(oldscrlp)
- ScrollPtr oldscrlp;
- {
- if(oldscrlp->vScrollHdl!=NIL)
- HiliteControl(oldscrlp->vScrollHdl,Inactive);
- if(oldscrlp->hScrollHdl!=NIL)
- HiliteControl(oldscrlp->hScrollHdl,Inactive);
- }
-
- /* Call when scrolling window changes size.
- Note: assumes growing window is active window */
-
- GrowScroll(theWindow)
- WindowPtr theWindow;
- {
- int theLineSize,hasOtherBar;
- int newTop,newRight,newBottom,newLeft;
- Rect tempRect;
-
- if(scrlp->vScrollHdl!=NIL)
- { /* Move vertical Bar */
- newTop=theWindow->portRect.top-1+
- scrlp->vScrollMarg;
- newRight=theWindow->portRect.right-SBarWidth;
- newBottom=theWindow->portRect.bottom-SBarWidth;
- MoveControl(scrlp->vScrollHdl,newRight,newTop);
- SizeControl(scrlp->vScrollHdl,SBarWidth+1,
- newBottom-newTop+1);
-
- /* Reset vertical scrolling rectangle */
- theLineSize=GetCRefCon(scrlp->vScrollHdl);
- hasOtherBar=(scrlp->hScrollHdl!=NIL)?TRUE:FALSE;
- SetVertRect(scrlp,theWindow,theLineSize,
- hasOtherBar);
-
- /* Reset vertical scroll bar range */
- SetScroll(scrlp,scrlp->vertLines,VertBar);
- }
-
- if(scrlp->hScrollHdl!=NIL)
- { /* Move horizontal Bar */
- newTop=theWindow->portRect.bottom-SBarWidth;
- newLeft=theWindow->portRect.left-1+
- scrlp->hScrollMarg;
- newRight=theWindow->portRect.right-SBarWidth;
- MoveControl(scrlp->hScrollHdl,newLeft,newTop);
- SizeControl(scrlp->hScrollHdl,newRight-newLeft+1,
- SBarWidth+1);
-
- /* Reset vertical scrolling rectangle */
- theLineSize=-GetCRefCon(scrlp->hScrollHdl);
- hasOtherBar=(scrlp->vScrollHdl!=NIL)?TRUE:FALSE;
- SetHorizRect(scrlp,theWindow,theLineSize,
- hasOtherBar);
-
- /* Reset vertical scroll bar range */
- SetScroll(scrlp,scrlp->horizLines,HorizBar);
- }
-
- /* Reset view rectangle (if text window) */
- if(scrlp->hTE!=NIL)
- { ScrollSectRect(scrlp,theWindow,&tempRect);
- (**scrlp->hTE).viewRect=tempRect;
- }
- }
-
- /* Invalidate the scroll bar area before and after
- window resizing */
-
- InvalBars(theWindow)
- WindowPtr theWindow;
- {
- Rect r;
-
- r=theWindow->portRect; /*Horizontal ScrollBar*/
- r.top=r.bottom-SBarWidth;
- InvalRect(&r);
- r.top=theWindow->portRect.top; /*Vertical Scroll Bar*/
- r.left=r.right-SBarWidth;
- InvalRect(&r);
- }
-
- /* Reset a scroll bar range when the document changes
- size. Updates window if necessary.
- 1. totalLines: total Lines in document
- 2. barType: VertBar or HorizBar */
-
- SetScroll(theScrlp,totalLines,barType)
- ScrollPtr theScrlp;
- int totalLines,barType;
- {
- int n,max,currentValue,numLinesVis,tempLineSize,units;
- WindowPtr ctrlWindow;
- ControlHandle scrollHdl;
-
- if(barType==VertBar)
- { tempLineSize=GetCRefCon(theScrlp->vScrollHdl);
- numLinesVis=theScrlp->vertLinesVis;
- scrollHdl=theScrlp->vScrollHdl;
- theScrlp->vertLines=totalLines;
- }
- else
- { tempLineSize=-GetCRefCon(theScrlp->hScrollHdl);
- numLinesVis=theScrlp->horizLinesVis;
- scrollHdl=theScrlp->hScrollHdl;
- theScrlp->horizLines=totalLines;
- }
-
- n=totalLines-numLinesVis;
- currentValue=GetCtlValue(scrollHdl);
- max=(n>0 ? n : 0);
- SetCtlMax(scrollHdl,max);
-
- /* if necessary, scroll and redraw the window */
- if(currentValue>max)
- { ctrlWindow=(*scrollHdl)->contrlOwner;
- InvalRect(&ctrlWindow->portRect);
- if(theScrlp->hTE!=NIL)
- { units=tempLineSize*(currentValue-max);
- if(barType==VertBar)
- OffsetRect(&(**theScrlp->hTE).destRect,
- 0,units);
- else
- OffsetRect(&(**theScrlp->hTE).destRect,
- units,0);
- }
- }
- }
-
- /* Return current margins in pixels or in scrolling
- lines for window.
- 1. left or topMargin: margin returned in
- these variables
- 2. lm or tmType: set to inPixels (0) or
- inLines (1) for result in pixels
- or lines */
-
- GetMargins(theScrlp,leftMargin,lmType,topMargin,tmType)
- ScrollPtr theScrlp;
- int *leftMargin,*topMargin,lmType,tmType;
- {
- int theLineSize;
-
- if(theScrlp->vScrollHdl!=NIL)
- { *topMargin=GetCtlValue(theScrlp->vScrollHdl);
- if(tmType==inPixels)
- { theLineSize=GetCRefCon(theScrlp->vScrollHdl);
- *topMargin*=theLineSize;
- }
- }
- else
- *topMargin=0;
- if(theScrlp->hScrollHdl!=NIL)
- { *leftMargin=GetCtlValue(theScrlp->hScrollHdl);
- if(lmType==inPixels)
- { theLineSize=-GetCRefCon(theScrlp->hScrollHdl);
- *leftMargin*=theLineSize;
- }
- }
- else
- *leftMargin=0;
- }
-
- /* Find intersection of two scrolling rectangles, if no
- scroll bars, this rect will include the grow box
- 1. drawRect: pointer to resulting rectangle - this
- pointer also returned as function value */
-
- Rect *ScrollSectRect(theScrlp,theWindow,drawRect)
- ScrollPtr theScrlp;
- WindowPtr theWindow;
- Rect *drawRect;
- {
- NonScrollRect(theScrlp,theWindow,drawRect);
- drawRect->top=theScrlp->vRectTopLeft.v;
- if(theScrlp->hRectTopLeft.v>drawRect->top)
- drawRect->top=theScrlp->hRectTopLeft.v;
- drawRect->left=theScrlp->vRectTopLeft.h;
- if(theScrlp->hRectTopLeft.h>drawRect->left)
- drawRect->left=theScrlp->hRectTopLeft.h;
- return(drawRect);
- }
-
- /* Return handle for region that is intersection of two
- scrolling rectangles. If no scroll bars, region will
- exclude grow box */
-
- RgnHandle ScrollSectRgn(theScrlp,theWindow)
- ScrollPtr theScrlp;
- WindowPtr theWindow;
- {
- Rect r;
- RgnHandle drawRgn,tempRgn;
-
- ScrollSectRect(theScrlp,theWindow,&r);
- drawRgn=NewRgn();
- RectRgn(drawRgn,&r);
- if((theScrlp->vScrollHdl==NIL)&&
- (theScrlp->hScrollHdl==NIL))
- { tempRgn=NewRgn();
- r.left=theWindow->portRect.right-SBarWidth;
- r.top=theWindow->portRect.bottom-SBarWidth;
- r.right=theWindow->portRect.right;
- r.bottom=theWindow->portRect.bottom;
- RectRgn(tempRgn,&r);
- DiffRgn(drawRgn,tempRgn,drawRgn);
- DisposeRgn(tempRgn);
- }
- return(drawRgn);
- }
-
- /* Calculate rectangle that excludes the scroll bars
- 1. drawRect: pointer to resulting rectangle - this
- pointer also returned as function value */
-
- Rect *NonScrollRect(theScrlp,theWindow,drawRect)
- ScrollPtr theScrlp;
- WindowPtr theWindow;
- Rect *drawRect;
- {
- *drawRect=theWindow->portRect;
- if(theScrlp->vScrollHdl!=NIL)
- drawRect->right-=SBarWidth;
- if(theScrlp->hScrollHdl!=NIL)
- drawRect->bottom-=SBarWidth;
- return(drawRect);
- }
-
- /* Draw enough grow icon to enclose active scroll bars */
-
- ScrollDrawGrowIcon(theScrlp,theWindow)
- ScrollPtr theScrlp;
- WindowPtr theWindow;
- {
- Rect r;
- RgnHandle tempRgn;
-
- GetClip(tempRgn=NewRgn());
- r=theWindow->portRect;
- if(theScrlp->vScrollHdl==NIL)
- r.top=r.bottom-SBarWidth;
- if(theScrlp->hScrollHdl==NIL)
- r.left=r.right-SBarWidth;
- ClipRect(&r);
- DrawGrowIcon(theWindow);
- SetClip(tempRgn);
- DisposeRgn(tempRgn);
- }
-
- /* Action procedure for scrolling - internal use
- Scroll by scrollAmt (set by doScroll)
- ScrollCode is the part of the scroll bar being tracked
- (set by doScroll) ScrollType is type of scroll bar
- (set by doScroll)
- May also be called for tracking selections. In this
- case scrollType will be set by setSelectScroll,
- scrollAmt will be 1 or -1 (set by selectScroll)
- and scrollCode will be set to 0. */
-
- pascal void ScrollProc(control,theCode)
- ControlHandle control;
- int theCode;
- {
- int max,oldValue,newValue;
- WindowPtr ctrlWindow=(*control)->contrlOwner;
-
- if(theCode==scrollCode)
- { oldValue=GetCtlValue(control);
- newValue=oldValue+scrollAmt;
- if(newValue<0) newValue=0;
- max=GetCtlMax(control);
- if(newValue>max) newValue=max;
- SetCtlValue(control,newValue);
- ThumbMove(ctrlWindow,oldValue,newValue);
- }
- }
-
- /* Do scrolling, return TRUE if scrolled or FALSE if not
- 1. where: mouse location in Global coordinates
- Note: assumes scrolling in current active window */
-
- DoScroll(theWindow,where)
- WindowPtr theWindow;
- long where;
- {
- int partCode,oldValue,newValue;
- ControlHandle control;
-
- GlobalToLocal(&where);
- partCode=FindControl(where,theWindow,&control);
- if(partCode==0) /*mouse down not in scroll bar*/
- return(FALSE);
-
- lineSize=GetCRefCon(control);
- if(lineSize<0) /* horiz scrolling */
- { linesVis=scrlp->horizLinesVis;
- scrollType=HorizBar;
- lineSize=-lineSize;
- }
- else /* vert scrolling */
- { linesVis=scrlp->vertLinesVis;
- scrollType=VertBar;
- }
-
- switch(partCode)
- { case inUpButton:
- case inDownButton:
- case inPageUp:
- case inPageDown:
- switch(partCode)
- { case inUpButton:
- scrollAmt=-1;
- break;
- case inDownButton:
- scrollAmt=1;
- break;
- case inPageUp:
- scrollAmt=-linesVis;
- break;
- case inPageDown:
- scrollAmt=linesVis;
- break;
- }
- scrollCode=partCode;
- TrackControl(control,where,&ScrollProc);
- break;
- case inThumb:
- oldValue=GetCtlValue(control);
- if(TrackControl(control,where,-1L))
- { newValue=GetCtlValue(control);
- ThumbMove(theWindow,oldValue,newValue);
- }
- break;
- default:
- break;
- }
- return(TRUE);
- }
-
- /* Set to prepare for automatic scrolling of scroll bars
- - internal use */
-
- SetSelectScroll(control)
- ControlHandle control;
- {
- lineSize=GetCRefCon(control);
- if(lineSize<0) /* horiz scrolling */
- { linesVis=scrlp->horizLinesVis;
- scrollType=HorizBar;
- lineSize=-lineSize;
- }
- else /* vert scrolling */
- { linesVis=scrlp->vertLinesVis;
- scrollType=VertBar;
- }
- scrollCode=0;
- }
-
- /* Scroll dir lines using scroll bar set by
- setSelectScroll - internal use */
-
- SelectScroll(dir)
- int dir;
- {
- scrollAmt=dir;
- if(scrollType==HorizBar)
- ScrollProc(scrlp->hScrollHdl,0);
- else
- ScrollProc(scrlp->vScrollHdl,0);
- }
-
- /* Scroll along with mouse while mouse button is down
- 1. where: mouse location in Global coordinates
- Note: assumes scrolling in current active window */
-
- DragScroll(where)
- Point where;
- {
- WindowPtr theWindow;
- register int vLineSize=0,hLineSize=0,vAmt,hAmt;
- Rect dragRect;
- Point lastWhere;
-
- if(scrlp->vScrollHdl!=NIL)
- { theWindow=(*(scrlp->vScrollHdl))->contrlOwner;
- vLineSize=GetCRefCon(scrlp->vScrollHdl);
- }
- if(scrlp->hScrollHdl!=NIL)
- { theWindow=(*(scrlp->hScrollHdl))->contrlOwner;
- hLineSize=-GetCRefCon(scrlp->hScrollHdl);
- }
- if((vLineSize==0)&&(hLineSize==0)) return;
-
- ScrollSectRect(scrlp,theWindow,&dragRect);
- GlobalToLocal(&where);
- if(!PtInRect(where,&dragRect)) return;
- lastWhere=where;
- while(StillDown())
- { GetMouse(&where);
- if(PtInRect(where,&dragRect))
- { if(vLineSize>0) /* vert scrolling */
- { if(vAmt=(lastWhere.v-where.v)/vLineSize)
- { SetSelectScroll(scrlp->vScrollHdl);
- SelectScroll(vAmt);
- lastWhere.v-=vAmt*vLineSize;
- }
- }
- if(hLineSize>0) /* horiz scrolling */
- { if(hAmt=(lastWhere.h-where.h)/hLineSize)
- { SetSelectScroll(scrlp->hScrollHdl);
- SelectScroll(hAmt);
- lastWhere.h-=hAmt*hLineSize;
- }
- }
- }
- }
- }
-
- /* Do joystick scrolling as long as mouse is down
- 1. where: mouse location in Global coordinates
- Note: assumes scrolling in current active window */
-
- JoyStickScroll(where)
- Point where;
- {
- int vAmt,hAmt;
- Point newWhere;
-
- if((scrlp->vScrollHdl==NIL)&&(scrlp->hScrollHdl==NIL))
- return;
- GlobalToLocal(&where);
- while(StillDown())
- { GetMouse(&newWhere);
- vAmt=(newWhere.v-where.v)/vJoySpeed;
- if((scrlp->vScrollHdl!=NIL)&&vAmt)
- { SetSelectScroll(scrlp->vScrollHdl);
- SelectScroll(vAmt);
- }
- hAmt=(newWhere.h-where.h)/hJoySpeed;
- if((scrlp->hScrollHdl!=NIL)&&hAmt)
- { SetSelectScroll(scrlp->hScrollHdl);
- SelectScroll(hAmt);
- }
- }
- }
-
- /* Set joy stick speed in pixels per speed increment
- 1. v or hSpeed: vertical or horizontal settings */
-
- SetJoySpeed(vSpeed,hSpeed)
- int vSpeed,hSpeed;
- {
- vJoySpeed=vSpeed;
- hJoySpeed=hSpeed;
- }
-
- /* If mouse is outside content region, scroll 1 line
- towards mouse location and return TRUE,
- otherwise just return FALSE
- Note: assumes scrolling in current active window */
-
- FollowMouse()
- {
- Point where;
- RgnHandle tempRgn;
- int followed=FALSE;
-
- GetMouse(&where);
- GetClip(tempRgn=NewRgn());
- ClipRect(&thePort->portRect);
-
- if(scrlp->hScrollHdl!=NIL) /* horiz tracking */
- { if(where.h<scrlp->horizScrollRect.left)
- { SetSelectScroll(scrlp->hScrollHdl);
- SelectScroll(-1);
- followed=TRUE;
- }
- else if(where.h>scrlp->horizScrollRect.right)
- { SetSelectScroll(scrlp->hScrollHdl);
- SelectScroll(1);
- followed=TRUE;
- }
- }
- if(scrlp->vScrollHdl!=NIL) /* vert tracking */
- { if(where.v<scrlp->vertScrollRect.top)
- { SetSelectScroll(scrlp->vScrollHdl);
- SelectScroll(-1);
- followed=TRUE;
- }
- else if(where.v>scrlp->vertScrollRect.bottom)
- { SetSelectScroll(scrlp->vScrollHdl);
- SelectScroll(1);
- followed=TRUE;
- }
- }
-
- SetClip(tempRgn);
- DisposeRgn(tempRgn);
- return(followed);
- }
-
- /* Handle automatic scrolling of Text Edit Windows
- Note: assumes scrolling in current active window */
-
- pascal Boolean SMClikLoop()
- {
- Point where;
- RgnHandle tempRgn;
-
- GetMouse(&where);
- if(PtInRect(where,&(**scrlp->hTE).viewRect))
- return(TRUE);
- GetClip(tempRgn=NewRgn());
- ClipRect(&thePort->portRect);
-
- if(scrlp->hScrollHdl!=NIL) /* horiz tracking */
- { if(where.h<(**scrlp->hTE).viewRect.left)
- { SetSelectScroll(scrlp->hScrollHdl);
- SelectScroll(-1);
- }
- else if(where.h>(**scrlp->hTE).viewRect.right)
- { SetSelectScroll(scrlp->hScrollHdl);
- SelectScroll(1);
- }
- }
- if(scrlp->vScrollHdl!=NIL) /* vert tracking */
- { if(where.v<(**scrlp->hTE).viewRect.top)
- { SetSelectScroll(scrlp->vScrollHdl);
- SelectScroll(-1);
- }
- else if(where.v>(**scrlp->hTE).viewRect.bottom)
- { SetSelectScroll(scrlp->vScrollHdl);
- SelectScroll(1);
- }
- }
-
- SetClip(tempRgn); /* must restore clip region */
- DisposeRgn(tempRgn);
- return(TRUE);
- }
-
- /* Scroll window from oldValue to newValue
- - internal use */
-
- ThumbMove(theWindow,oldValue,newValue)
- WindowPtr theWindow;
- int oldValue,newValue;
- {
- int units=oldValue-newValue;
-
- if(units)
- ScrollWindow(theWindow,units);
- }
-
- /* Do the actual scrolling. When required this routine
- calls UpdateWindow() which must be supplied by the
- application - internal use */
-
- ScrollWindow(theWindow,units)
- WindowPtr theWindow;
- int units;
- {
- int absunits,rectLength;
- Boolean hasRuler=FALSE;
- RgnHandle tmpRgn;
- GrafPtr saveport;
- Rect rulerRect;
-
- GetPort(&saveport);
- SetPort(theWindow);
- absunits= units<0 ? -units : units;
-
- /* scroll text windows */
- if(scrlp->hTE!=NIL)
- { if(scrollType==HorizBar)
- { TEScroll(rectLength=lineSize*units,0,
- scrlp->hTE);
- if(scrlp->vRectTopLeft.v!=
- scrlp->hRectTopLeft.v)
- { hasRuler=TRUE;
- rulerRect.left=scrlp->hRectTopLeft.h;
- rulerRect.top=scrlp->hRectTopLeft.v;
- rulerRect.bottom=scrlp->vRectTopLeft.v;
- rulerRect.right=
- scrlp->horizScrollRect.right;
- }
- }
- else
- { TEScroll(0,rectLength=lineSize*units,
- scrlp->hTE);
- if(scrlp->vRectTopLeft.h!=
- scrlp->hRectTopLeft.h)
- { hasRuler=TRUE;
- rulerRect.left=scrlp->vRectTopLeft.h;
- rulerRect.top=scrlp->vRectTopLeft.v;
- rulerRect.right=scrlp->hRectTopLeft.h;
- rulerRect.bottom=
- scrlp->vertScrollRect.bottom;
- }
- }
- /* if rulers in text windows, scroll them */
- if(hasRuler)
- { tmpRgn=NewRgn();
- if(scrollType==HorizBar)
- ScrollRect(&rulerRect,lineSize*units,0,
- tmpRgn);
- else
- ScrollRect(&rulerRect,0,lineSize*units,
- tmpRgn);
- InvalRgn(tmpRgn);
- DisposeRgn(tmpRgn);
- }
- }
-
- /* Scolling more than 1 page in graphic windows */
- else if(absunits>linesVis)
- { if(scrollType==HorizBar)
- InvalRect(&scrlp->horizScrollRect);
- else
- InvalRect(&scrlp->vertScrollRect);
- }
-
- /* Fractional page scolling in graphic windows */
- else
- { tmpRgn=NewRgn();
- if(scrollType==HorizBar)
- ScrollRect(&scrlp->horizScrollRect,
- lineSize*units,0,tmpRgn);
- else
- ScrollRect(&scrlp->vertScrollRect,
- 0,lineSize*units,tmpRgn);
- InvalRgn(tmpRgn);
- DisposeRgn(tmpRgn);
- }
-
- /* If required, call Update process */
- if((scrlp->hTE==NIL)||hasRuler)
- UpdateWindow(theWindow,scrollType);
-
- SetPort(saveport);
- }
-
- /* Scroll the line and column numbers given by selLine
- and selColumn into view
- Note: assumes scrolling in current active window */
-
- FixInsertPt(selLine,selColumn)
- int selLine,selColumn;
- {
- if(scrlp->vScrollHdl!=NIL)
- MoveInsertPt(selLine,VertBar);
- if(scrlp->hScrollHdl!=NIL)
- MoveInsertPt(selColumn,HorizBar);
- }
-
- /* Scroll begining of selection of Text Edit window into
- view
- Note: assumes scrolling in current active window */
-
- FixTEInsertPt()
- {
- int selLine,selColumn=0,textLength,*line0;
- WindowPtr TEWindow;
- GrafPtr saveGraf;
- register int thePt,jumpSize,lineLook;
-
- if(scrlp->hTE==NIL) return;
- if(scrlp->vScrollHdl!=NIL)
- TEWindow=(*(scrlp->vScrollHdl))->contrlOwner;
- else if(scrlp->hScrollHdl!=NIL)
- TEWindow=(*(scrlp->hScrollHdl))->contrlOwner;
- else
- return;
-
- /* Binary search for current line */
- thePt=(**scrlp->hTE).selStart; /*Set up pointers*/
- lineLook=(**scrlp->hTE).nLines;
- line0=&(**scrlp->hTE).lineStarts[0];
- if(thePt>=*(line0+lineLook)) /*Check last line*/
- selLine=lineLook-1;
- else
- { jumpSize=lineLook>>1|1; /*div by 2, but not 0*/
- lineLook-=jumpSize;
- while(jumpSize=jumpSize>>1|1) /*reduce jumpsize*/
- { if(thePt<*(line0+lineLook))
- lineLook-=jumpSize; /*look back*/
- else if(thePt>=*(line0+lineLook+1))
- lineLook+=jumpSize; /*look forward*/
- else
- { selLine=lineLook; /*found line*/
- break;
- }
- }
- }
-
- /* Find column by getting width of text from start of
- line to insertion point. */
- if(scrlp->hScrollHdl!=NIL)
- { GetPort(&saveGraf);
- SetPort(TEWindow);
- HLock((**scrlp->hTE).hText);
- textLength=thePt-
- (**scrlp->hTE).lineStarts[selLine];
- selColumn=TextWidth(*((**scrlp->hTE).hText),
- (**scrlp->hTE).lineStarts[selLine],
- textLength);
- HUnlock((**scrlp->hTE).hText);
- selColumn/=(-GetCRefCon(scrlp->hScrollHdl));
- SetPort(saveGraf);
- }
-
- /* Correct selLine in case scrolling line is smaller
- than text lineHeight */
- if(GetCRefCon(scrlp->vScrollHdl)<
- (**scrlp->hTE).lineHeight)
- { selLine=selLine*(**scrlp->hTE).lineHeight/
- GetCRefCon(scrlp->vScrollHdl);
- if(selLine>GetCtlValue(scrlp->vScrollHdl))
- selLine+=1+((**scrlp->hTE).lineHeight/
- GetCRefCon(scrlp->vScrollHdl));
- }
- FixInsertPt(selLine,selColumn);
- }
-
- /* Scoll one direction to bring point into view
- - internal use */
-
- MoveInsertPt(selStart,barType)
- int selStart,barType;
- {
- int curValue,theLinesVis,units;
- ControlHandle scrollHdl;
-
- if(barType==VertBar)
- { theLinesVis=scrlp->vertLinesVis;
- scrollHdl=scrlp->vScrollHdl;
- }
- else
- { theLinesVis=scrlp->horizLinesVis;
- scrollHdl=scrlp->hScrollHdl;
- }
-
- curValue=GetCtlValue(scrollHdl);
- if(selStart<curValue)
- units=selStart-curValue;
- else if(selStart>(curValue+theLinesVis-1))
- units=selStart-curValue-theLinesVis+1;
- else
- return;
-
- SetSelectScroll(scrollHdl);
- SelectScroll(units);
- }
-
- /* Call TEClick - call from here insures SMClikLoop Code
- available. Meaning of parameters identical to
- Toolbox routine TEClick() */
-
- ScrollTEClick(where,theShift,hTE)
- Point where;
- Boolean theShift;
- TEHandle hTE;
- {
- TEClick(where,theShift,hTE);
- }
-
- /* If ScrollInfo in heap, dispose of pointer when done.
- Do not call if ScrollInfo not in heap*/
-
- DisposeScroll(theScrlp)
- ScrollPtr theScrlp;
- {
- DisposPtr(theScrlp);
- }
-
- /* Return the refCon in ScrollInfo record */
-
- long GetSRefCon(theScrlp)
- ScrollPtr theScrlp;
- {
- return(theScrlp->refCon);
- }
-
- /* Set the refCon in ScrollInfo record */
-
- SetSRefCon(theScrlp,sRefCon)
- ScrollPtr theScrlp;
- long sRefCon;
- {
- theScrlp->refCon=sRefCon;
- }
-
- /* Return the current active ScrollPtr */
-
- ScrollPtr ActiveScrollPtr()
- {
- return(scrlp);
- }
-